﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Windows.Forms.DataVisualization.Charting;

namespace MarketFunds
{
    public partial class frmChart : Form
    {
        public frmChart()
        {
            InitializeComponent();
        }
        private void chart_GetToolTipText(object sender, ToolTipEventArgs e)
        {
            if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
            {
                int i = e.HitTestResult.PointIndex;
                DateTime dt = (DateTime)chartMain.Series[0].Points[i].Tag;
                string strDayOfWeek = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dt.DayOfWeek);
                string strText = string.Format("日期:{0}\r\n{1}:{2}\r\n{3}:{4}\r\n{5}:{6}\r\n{7}:{8}\r\n{9}:{10}" , 
                                                           dt.ToShortDateString() +" " + strDayOfWeek,
                                                           chartMain.Series[0].LegendText, chartMain.Series[0].Points[i].YValues[0],
                                                           chartMain.Series[1].LegendText, chartMain.Series[1].Points[i].YValues[0],
                                                           chartMain.Series[2].LegendText, chartMain.Series[2].Points[i].YValues[0],
                                                           chartMain.Series[3].LegendText, chartMain.Series[3].Points[i].YValues[0],
                                                           chartMain.Series[4].LegendText, chartMain.Series[4].Points[i].YValues[0]);
                e.Text = strText;//chartMain.Series[0].Points[i].AxisLabel;
                //DataPoint dp = e.HitTestResult.Series.Points[i];
                //e.Text = string.Format("{1:F3}", dp.XValue, dp.YValues[0]);
                
                //e.Text = 
            }
        }
        /// <summary>
        /// 在0轴正负0.5位置增加一条线，代表主力资金的生命线，未必用
        /// </summary>
        private void addHalfPoint_line()
        {
            ///在0轴0.5位置增加一条线，代表主力资金的生命线
            StripLine stripline = new StripLine();
            stripline.Interval = 0;
            stripline.IntervalOffset = 0.5;
            stripline.StripWidth = 0.01;
            stripline.BackColor = Color.Red;
            stripline.BorderDashStyle = ChartDashStyle.NotSet;
            chartMain.ChartAreas[0].AxisY.StripLines.Add(stripline);
            

            StripLine stripline2 = new StripLine();
            stripline2.Interval = 0;
            stripline2.IntervalOffset = -0.5;
            stripline2.StripWidth = 0.01;
            stripline2.BackColor = Color.Green;
            stripline2.BorderDashStyle = ChartDashStyle.NotSet;
            chartMain.ChartAreas[0].AxisY.StripLines.Add(stripline2);
        }
        /// <summary>
        /// 装载指数价格
        /// </summary>
        private void LoadPrice()
        {
            DateTime dToday = DateTime.Now;
            DateTime dLastDay = dToday.AddMonths(-2);//上一个时间是前两个月

            chartMain.Series.Clear();

            chartMain.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //设置网格类型为虚线
            chartMain.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash; //设置网格类型为虚线
            chartMain.ChartAreas[0].Position.X = 0;
            chartMain.ChartAreas[0].Position.Y = 0;
            
            chartMain.Series.Add("主力资金(百亿)");
            chartMain.Series[0].ChartType = SeriesChartType.Line;
            chartMain.Series[0].BorderColor = Color.Red;
            chartMain.Series[0].LegendText = "主力资金(百亿)";
            chartMain.Series[0].IsValueShownAsLabel = true;

            chartMain.Series.Add("上证指数");
            chartMain.Series[1].ChartType = SeriesChartType.Line;
            chartMain.Series[1].LegendText = "上证指数/3000";
            chartMain.Series.Add("深证成指");
            chartMain.Series[2].ChartType = SeriesChartType.Line;
            chartMain.Series[2].LegendText = "深证成指/10000";
            chartMain.Series.Add("创业板指");
            chartMain.Series[3].ChartType = SeriesChartType.Line;
            chartMain.Series[3].LegendText = "创业板指/2000";
            int nPos = 0;
            string strSql = string.Format("select top 50 * from MarketFundsHistory where hddate >=#{0}# and hddate<=#{1}# order by id ", dLastDay.ToShortDateString(), dToday.ToShortDateString());
            OleDbCommand cmd = new OleDbCommand(strSql, StockExcel.CommonUtil.dbconn);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                DateTime dt = (DateTime)reader["hddate"];
                double dZL = (double)reader["zlFunds"] / 100;  //百亿，除以100;
                double dSZZS = (double)reader["SZZS"] / 1000 ; //永远3000点
                double dSZCZ = (double)reader["SZCZ"] / 1000;   //永远10000点
                double dCYBZ = (double)reader["CYBZ"] / 1000;    //永远2000点
                chartMain.Series[0].Points.AddXY(dt, dZL.ToString("###,###.##"));
                chartMain.Series[0].Points[nPos].Tag = dt;
                //chartMain.Series[0].Points[nPos].AxisLabel = dt.ToShortDateString();
                chartMain.Series[1].Points.AddXY(dt, dSZZS);
                chartMain.Series[2].Points.AddXY(dt, dSZCZ);
                chartMain.Series[3].Points.AddXY(dt, dCYBZ);
                nPos++;
            }
            reader.Close();
            cmd.Dispose();

            chartMain.GetToolTipText += new EventHandler<ToolTipEventArgs>(chart_GetToolTipText);
        }
        /// <summary>
        /// 以指数涨幅来装载
        /// </summary>
        private void LoadRate()
        {
            DateTime dToday = DateTime.Now;
            DateTime dLastDay = dToday.AddMonths(-2);//上一个时间是前两个月

            chartMain.Series.Clear();
            chartMain.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //设置网格类型为虚线
            chartMain.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash; //设置网格类型为虚线
            chartMain.Series.Add("主力资金(百亿)");
            chartMain.Series[0].ChartType = SeriesChartType.Line;
            chartMain.Series[0].BorderColor = Color.Red;
            chartMain.Series[0].LegendText = "主力资金(百亿)";
            chartMain.Series[0].IsValueShownAsLabel = true;

            chartMain.Series.Add("上证指数");
            chartMain.Series[1].ChartType = SeriesChartType.Line;
            chartMain.Series[1].LegendText = "上证指数";
            chartMain.Series.Add("深证成指");
            chartMain.Series[2].ChartType = SeriesChartType.Line;
            chartMain.Series[2].LegendText = "深证成指";
            chartMain.Series.Add("创业板指");
            chartMain.Series[3].ChartType = SeriesChartType.Line;
            chartMain.Series[3].LegendText = "创业板指";

            chartMain.Series.Add("逆回购净投放");
            chartMain.Series[4].ChartType = SeriesChartType.Line;
            chartMain.Series[4].LegendText = "购回购净投放";

            int nPos = 0;
            string strSql = string.Format("select top 50 * from MarketFundsHistory where hddate >=#{0}# and hddate<=#{1}# order by id ", dLastDay.ToShortDateString(), dToday.ToShortDateString());
            OleDbCommand cmd = new OleDbCommand(strSql, StockExcel.CommonUtil.dbconn);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                DateTime dt = (DateTime)reader["hddate"];
                double dZL = (double)reader["zlFunds"] / 100;  //百亿，除以100;
                double dSZZS = (double)reader["SZZS_RATE"];
                double dSZCZ = (double)reader["SZCZ_RATE"];
                double dCYBZ = (double)reader["CYBZ_RATE"];
                double dNHG = StockExcel.CommonUtil.Get当日净投放(dt) / 1000;

                chartMain.Series[0].Points.AddXY(dt, dZL.ToString("###,###.##"));
                chartMain.Series[0].Points[nPos].Tag = dt;
                //chartMain.Series[0].Points[nPos].AxisLabel = dt.ToShortDateString();
                chartMain.Series[1].Points.AddXY(dt, dSZZS);
                chartMain.Series[2].Points.AddXY(dt, dSZCZ);
                chartMain.Series[3].Points.AddXY(dt, dCYBZ);
                chartMain.Series[4].Points.AddXY(dt, dNHG);
                nPos++;
            }
            reader.Close();
            cmd.Dispose();

            chartMain.GetToolTipText += new EventHandler<ToolTipEventArgs>(chart_GetToolTipText);
            //addHalfPoint_line();
        }
        private void frmChart_Load(object sender, EventArgs e)
        {
            LoadRate();
            timer1.Enabled = true;
        }

        private void 按指数价格装载ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadPrice();
        }

        private void 按指数涨跌幅装载ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadRate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            /*
            if (StockExcel.CommonUtil.IsInTradingTime())
            {
                string strSql = "select * from MarketFundsHistory order by hddate desc";
                OleDbCommand cmd = new OleDbCommand(strSql, StockExcel.CommonUtil.dbconn);
                OleDbDataReader reader = cmd.ExecuteReader();
            }
            */
            timer1.Enabled = true;
        }
        public object GetPropertyValue(object info, string field)
        {
            if (info == null) return null;
            Type t = info.GetType();
            IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
            return property.First().GetValue(info, null);
        }
        private void chartMain_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                HitTestResult Result = new HitTestResult();
                Result = chartMain.HitTest(e.X, e.Y);
                if (Result.Series != null && Result.Object != null)
                {
                    /*
                    // 获取当前焦点x轴的值
                    string xValue = GetPropertyValue(Result.Object, "AxisLabel").ToString();
                    // 获取当前焦点所属区域名称
                    string areaName = GetPropertyValue(Result.Object, "LegendText").ToString();
                    // 获取当前焦点y轴的值
                    double yValue = Result.Series.Points[Result.PointIndex].YValues[0];
                    */
                    txtInfo.Visible = true;
                    int i = Result.PointIndex;
                    DateTime dt = (DateTime)chartMain.Series[0].Points[i].Tag;
                    string strDayOfWeek = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(dt.DayOfWeek);
                    string strText = string.Format("日期:{0}\r\n{1}:{2}\r\n{3}:{4}\r\n{5}:{6}\r\n{7}:{8}\r\n{9}:{10}",
                                                               dt.ToShortDateString() + " " + strDayOfWeek,
                                                               chartMain.Series[0].LegendText, chartMain.Series[0].Points[i].YValues[0],
                                                               chartMain.Series[1].LegendText, chartMain.Series[1].Points[i].YValues[0],
                                                               chartMain.Series[2].LegendText, chartMain.Series[2].Points[i].YValues[0],
                                                               chartMain.Series[3].LegendText, chartMain.Series[3].Points[i].YValues[0],
                                                               chartMain.Series[4].LegendText, chartMain.Series[4].Points[i].YValues[0]);
                    txtInfo.Text = strText;
                }
                else
                {
                    // 鼠标离开时label隐藏
                    txtInfo.Text = "";
                    txtInfo.Visible = false;
                }
            }
            catch (Exception se)
            {
                txtInfo.Text = "";
                txtInfo.Visible = false;
            }
        }
    }
}
